package net.vanchu.mortgagecalculator.mortgage.calculate.mix;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.widget.Toast;

import com.dtspread.apps.houseloan.R;

import net.vanchu.mortgagecalculator.common.commonView.call.PopupWindowHelper;
import net.vanchu.mortgagecalculator.common.commonView.component.SelectingOptionsLayout;
import net.vanchu.mortgagecalculator.common.commonView.component.WheelView;
import net.vanchu.mortgagecalculator.common.commonView.component.WritingOptionLayout;
import net.vanchu.mortgagecalculator.common.config.LoanRate;
import net.vanchu.mortgagecalculator.common.config.LoanWay;
import net.vanchu.mortgagecalculator.common.config.OptionsProvider;
import net.vanchu.mortgagecalculator.mortgage.calculate.CalculateResultListener;
import net.vanchu.mortgagecalculator.mortgage.calculate.OnLoanWaySwitchListener;

import java.util.HashMap;
import java.util.List;

/**
 * Created by Granton on 2016/12/19.
 */
public class CalMixLoansFragment extends Fragment implements CalMixLoanContract.View {

    private static final String TAG = CalMixLoansFragment.class.getSimpleName();

    private View parentView;

    private SelectingOptionsLayout layoutLoansWay;

    private SelectingOptionsLayout layoutLoanYears;

    private WritingOptionLayout layoutComLoanValue;

    private SelectingOptionsLayout layoutComRates;

    private WritingOptionLayout layoutProLoanValue;

    private SelectingOptionsLayout layoutProRates;

    private TextView btnApplyCal;

    private CalMixLoanContract.Presenter presenter;

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        presenter = new CalMixLoanPresenter(
                this, new MixLoanWayInputData(),
                (OnLoanWaySwitchListener) getActivity(),
                (CalculateResultListener) getActivity()
        );
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        parentView = inflater.inflate(R.layout.fragment_cal_mix_loans, container, false);
        initViews(parentView);
        return parentView;
    }

    private void initViews(View parentView) {
        layoutLoansWay = (SelectingOptionsLayout) parentView.findViewById(R.id.layout_loans_way);
        layoutLoansWay.setRightTextViewText(OptionsProvider.convertLoanWayToStringShow(LoanWay.wayMix));
        layoutLoansWay.setRightTextViewFontColor(R.color.txt_option_main_color);
        layoutLoansWay.setRightTextViewClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.preChooseLoanWay();
            }
        });

        layoutLoanYears = (SelectingOptionsLayout) parentView.findViewById(R.id.layout_loans_years);
        layoutLoanYears.setRightTextViewClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.preChooseLoanYear();
            }
        });

        layoutComLoanValue = (WritingOptionLayout) parentView.findViewById(R.id.layout_com_loan_value);
        layoutComLoanValue.setInputValidator(presenter.asInputValidator());
        layoutComLoanValue.setInputStateListener(presenter.asInputStateListener());

        layoutComRates = (SelectingOptionsLayout) parentView.findViewById(R.id.layout_com_loans_rates);
        layoutComRates.setRightTextViewClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.preChooseComLoanRates();
            }
        });

        layoutProLoanValue = (WritingOptionLayout) parentView.findViewById(R.id.layout_pro_loan_value);
        layoutProLoanValue.setInputValidator(presenter.asInputValidator());
        layoutProLoanValue.setInputStateListener(presenter.asInputStateListener());

        layoutProRates = (SelectingOptionsLayout) parentView.findViewById(R.id.layout_pro_loans_rates);
        layoutProRates.setRightTextViewClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                presenter.preChooseProLoanRates();
            }
        });

        btnApplyCal = (TextView) parentView.findViewById(R.id.btn_apply_cal);
        btnApplyCal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                presenter.applyCalculate();
            }
        });
    }

    @Override
    public void showLoanYear(String yearExpress) {
        layoutLoanYears.setRightTextViewText(yearExpress);
        layoutLoanYears.setRightTextViewFontColor(R.color.txt_option_main_color);
    }

    @Override
    public void showCommercialLoanRate(String rateExpress) {
        layoutComRates.setRightTextViewText(rateExpress);
        layoutComRates.setRightTextViewFontColor(R.color.txt_option_main_color);
    }

    @Override
    public void showProvidentFundRate(String rateExpress) {
        layoutProRates.setRightTextViewText(rateExpress);
        layoutProRates.setRightTextViewFontColor(R.color.txt_option_main_color);
    }

    @Override
    public void showEnableCalButton() {
        btnApplyCal.setEnabled(true);
        btnApplyCal.setBackgroundResource(R.drawable.button_cal);
    }

    @Override
    public void showDisableCalButton() {
        btnApplyCal.setEnabled(false);
        btnApplyCal.setBackgroundResource(R.drawable.bg_common_btn_disabled);
    }

    @Override
    public void showLoanWays() {
        List<String> optionNames = OptionsProvider.getLoansWayNames();
        PopupWindowHelper.callPopWinWithSingleWv(
                parentView, 0, optionNames, OptionsProvider.getMapLoansWay(),
                new WheelView.SingleWvCallBack<LoanWay>() {
                    @Override
                    public void onSelected(LoanWay loanWay) {
                        presenter.chooseLoanWay(loanWay);
                    }
                }
        );
    }

    @Override
    public void showLoanYears() {
        List<String> optionsName = OptionsProvider.getYearsStr();
        PopupWindowHelper.callPopWinWithSingleWv(
                parentView, 0, optionsName, OptionsProvider.getMapYear(),
                new WheelView.SingleWvCallBack<Integer>() {
                    @Override
                    public void onSelected(Integer year) {
                        presenter.chooseLoanYear(year);
                    }
                });
    }

    @Override
    public void showProvidentFundLoanRates() {
        List<String> optionNamesLeft = OptionsProvider.getRateNames();
        List<String> optionNamesRight = OptionsProvider.getDiscountNames();

        HashMap<String, LoanRate> optionsLeft = OptionsProvider.getMapLoansRate();
        HashMap<String, Float> rightOptions = OptionsProvider.getMapDiscount();

        PopupWindowHelper.callPopWinWithTwoWv(
                parentView, 0, OptionsProvider.DISCOUNT_DEFAULT_POSITION,
                optionNamesLeft, optionNamesRight, optionsLeft, rightOptions,
                new WheelView.TwoWvCallBack<LoanRate, Float>() {
                    @Override
                    public void onSelected(LoanRate loanRate, Float discount) {
                        presenter.chooseProLoanRates(loanRate, discount);
                    }
                });
    }

    @Override
    public void showCommercialLoanRates() {
        List<String> optionNamesLeft = OptionsProvider.getRateNames();
        List<String> optionNamesRight = OptionsProvider.getDiscountNames();

        HashMap<String, LoanRate> optionsLeft = OptionsProvider.getMapLoansRate();
        HashMap<String, Float> rightOptions = OptionsProvider.getMapDiscount();

        PopupWindowHelper.callPopWinWithTwoWv(
                parentView, 0, OptionsProvider.DISCOUNT_DEFAULT_POSITION,
                optionNamesLeft, optionNamesRight, optionsLeft, rightOptions,
                new WheelView.TwoWvCallBack<LoanRate, Float>() {
                    @Override
                    public void onSelected(LoanRate loanRate, Float discount) {
                        presenter.chooseComLoanRates(loanRate, discount);
                    }
                });
    }

    @Override
    public void tipError(String s) {
        Toast.makeText(getActivity(), s, Toast.LENGTH_SHORT).show();
    }

    @Override
    public void resetComLoanRates() {
        layoutComRates.setRightTextViewText(R.string.please_choose_loan_rate);
        layoutComRates.setRightTextViewFontColor(R.color.txt_option_sub_text);
    }

    @Override
    public void resetProLoanRates() {
        layoutProRates.setRightTextViewText(R.string.please_choose_loan_rate);
        layoutProRates.setRightTextViewFontColor(R.color.txt_option_sub_text);
    }

}